import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

//import datastructure.ConcurrentLinkedList;

/**
 * @author Yinzi Chen
 * @date Nov 16, 2013
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        final ConcurrentLinkedList<Integer> list = new ConcurrentLinkedList<Integer>();
        long t = System.currentTimeMillis();
        ExecutorService service = Executors.newFixedThreadPool(50);
        for (int i = 0; i < 1; ++i)
            service.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10000000; ++i)
                        list.offer(1);
                }
            });
        service.shutdown();
        service.awaitTermination(1, TimeUnit.HOURS);
        System.out.println(System.currentTimeMillis() - t);
        final AtomicInteger cnt = new AtomicInteger(0);
        service = Executors.newFixedThreadPool(8);
        t = System.currentTimeMillis();
        for (int i = 0; i < 10; ++i) {
            service.execute(new Runnable() {
                @Override
                public void run() {
                    while (list.poll() != null) {
                        cnt.incrementAndGet();
                        // System.out.println(cnt);
                    }
                }
            });
        }
        service.shutdown();
        service.awaitTermination(1, TimeUnit.HOURS);
        System.out.println(cnt);
        System.out.println(System.currentTimeMillis() - t);
    }
}
